package Others;

import java.util.Properties;
import java.util.Random;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Email {

    Random randomGenerator = new Random();
    String[] Vecto = {"UGA", "AB", "AC", "FA", "USA", "IKA", "TSA", "TAVF", "unaUJ", "TGxa", "oalE", "Sc", "UPlcB", "QTx", "AUJT", "LOIA", "OZA", "JNCA", "YRA", "Ars"};

    public Email() {
    }

    public String SendEmail_New_Password(String email_pass) {
        final String username = "plataformasdesenvolvimento@gmail.com";
        final String password = "deisIS3C";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session2 = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            Message message = new MimeMessage(session2);
            message.setFrom(new InternetAddress("plataformasdesenvolvimento@gmail.com"));


            String NewPass = new String();


            int randomInt = randomGenerator.nextInt(100);
            NewPass += "" + randomInt;

            randomInt = randomGenerator.nextInt(20);
            NewPass += Vecto[randomInt];

            randomInt = randomGenerator.nextInt(10000);
            NewPass += randomInt;

            randomInt = randomGenerator.nextInt(20);
            NewPass += Vecto[randomInt];


            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(email_pass));

            NewPass += randomInt;

            message.setSubject("Nova Password");
            message.setText("A sua nova password é: " + NewPass);

            Transport.send(message);

            return NewPass;

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

    }
}
